package dreamCookGame.chickenMiniGame;

public class ChickenEntity extends Entity{
	
	//remember: springForce = (1/2) k * (x*x)
	//remember: electromgneticForce = (k*q1*q2)/(r*r)
	Vector _target;
	Vector _acceleration;
	float _bufferRadius;
	float _springConstant;
	Entity _runFrom;
	Coop _myCoop;
	boolean _isCaught;

	ChickenEntity(String name, String[] nameArray, Entity runFrom, Coop coop) {
		super(name, nameArray);
		setVariables(runFrom, coop);
	}
	ChickenEntity(String name, String[] nameArray, float x, float y, Entity runFrom, Coop coop) {
		super(name, nameArray);
		this.setLocation(x, y);
		setVariables(runFrom, coop);
		_springConstant = 0.0001f;
	}
	private void setVariables(Entity runFrom, Coop coop) {
		_target = new Vector(this._location.getX(),this._location.getY());
		_acceleration = new Vector(0,0);
		_springConstant = 0.0001f;
		_runFrom = runFrom;
		_bufferRadius=10;
		_myCoop = coop;
		_isCaught = false;
		this.setFriction(1);
	}
	public void update(){
		if(!_isCaught){
			Vector differenceVector;
			float force;
			float sqrDistance;
			Vector forceVector;
			
			differenceVector = new Vector(this._location.getX(),this._location.getY());
			differenceVector.subtract(_runFrom._location);
			sqrDistance = differenceVector.getSquareMagnitude();
			differenceVector.normalize();
			force = 10000/(sqrDistance);
			forceVector = new Vector(differenceVector.getX()*force,differenceVector.getY()*force);
			this._velocity.add(forceVector);
			this.setVelocity(this._velocity);
			
			differenceVector = new Vector(this._location.getX(),this._location.getY());
			differenceVector.subtract(_target);
			sqrDistance = differenceVector.getSquareMagnitude();
			if(sqrDistance>(_bufferRadius*_bufferRadius)){
				this.setFriction(1);
				differenceVector.normalize();
				force = (0.5f)*_springConstant*(sqrDistance);
				forceVector = new Vector(-differenceVector.getX()*force,-differenceVector.getY()*force);
				this._velocity.add(forceVector);
				this.setVelocity(this._velocity);
			}else{
				this.setFriction(4);
			}
		}
		super.update();
	}
	public void setCaught(){
		_isCaught = true;
		this.setVelocity(0, 0);
		this._direction=Math.PI/2;
		this.setLocation(_myCoop._center);
	}
}
